package org.raymond.iworks.study.basic.thread.exchanger;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Exchanger;

@Slf4j
public class Demo1 {
    public static void main(String[] args) {
        test1();
    }

    // 两个线程互相交换数据
    public static void test1(){
        Exchanger<Object> exchanger = new Exchanger();
        Thread t1 = new Thread(()->{
            try {
                Object t2Val = exchanger.exchange(1);
                log.info("{} {}", t2Val, (t2Val instanceof String));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(()->{
            try {
                Object t1Val = exchanger.exchange("hello");
                log.info("{} {}", t1Val, (t1Val instanceof Integer));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();
    }
}
